package cn.xhy.material.service.depot;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.xhy.material.controller.admin.depot.vo.DepotPageReqVO;
import cn.xhy.material.controller.admin.depot.vo.DepotSaveReqVO;
import cn.xhy.material.dal.dataobject.depot.DepotDO;
import cn.xhy.material.dal.mysql.depot.DepotMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.xhy.material.enums.ErrorCodeConstants.DEPOT_NOT_EXISTS;

/**
 * 仓库 Service 实现类
 *
 * @author xhy
 */
@Service
@Validated
public class DepotServiceImpl implements DepotService {

    @Resource
    private DepotMapper depotMapper;

    @Override
    public Long createDepot(DepotSaveReqVO createReqVO) {
        // 插入
        DepotDO depot = BeanUtils.toBean(createReqVO, DepotDO.class);
        depotMapper.insert(depot);
        // 返回
        return depot.getId();
    }

    @Override
    public void updateDepot(DepotSaveReqVO updateReqVO) {
        // 校验存在
        validateDepotExists(updateReqVO.getId());
        // 更新
        DepotDO updateObj = BeanUtils.toBean(updateReqVO, DepotDO.class);
        depotMapper.updateById(updateObj);
    }

    @Override
    public void deleteDepot(Long id) {
        // 校验存在
        validateDepotExists(id);
        // 删除
        depotMapper.deleteById(id);
    }

    private void validateDepotExists(Long id) {
        if (depotMapper.selectById(id) == null) {
            throw exception(DEPOT_NOT_EXISTS);
        }
    }

    @Override
    public DepotDO getDepot(Long id) {
        return depotMapper.selectById(id);
    }

    @Override
    public PageResult<DepotDO> getDepotPage(DepotPageReqVO pageReqVO) {
        return depotMapper.selectPage(pageReqVO);
    }

}
